E-Tech Timer / E-Tech Timer / countdown.vbon commit add archived files (d846247)
   1
   2'All code written by Andrew Lorimer for E-Tech Software
   3
   4Imports System.Text.RegularExpressions
   5Public Class countdown
   6
   7    'DECLERATION OF VARIABLES
   8    Public startTime As DateTime 'Stores when the timer was started
   9    Public elapsedTime As TimeSpan 'Stores the remaning time
  10    Public hasBeenPaused As Boolean = False 'Determines whether the time has been paused
  11    Public startPause As DateTime 'Stores the time when the timer was paused
  12    Public elapsedPause As TimeSpan 'How long the current session of paused time has been going for
  13    Public elapsedPauseAddends As TimeSpan 'How long other sessions of paused time have been going for
  14    Public countDownFrom As TimeSpan
  15
  16
  17#Region "UI Events"
  18
  19    Private Sub KryptonButton10_Click(sender As Object, e As EventArgs) Handles KryptonButton10.Click
  20        'Show the stopwatch window and close the countdown window
  21        stopwatch.Show()
  22        stopwatch.Location = New Point(Location.X, Location.Y)
  23        stopwatch.Focus()
  24        Close()
  25    End Sub
  26
  27    Private Sub KryptonButton8_Click(sender As Object, e As EventArgs) Handles KryptonButton8.Click
  28        'Show the settings dialog
  29        settings.Show()
  30        settings.BringToFront()
  31    End Sub
  32
  33    Private Sub KryptonTextBox1_TextChanged(sender As Object, e As EventArgs) Handles KryptonTextBox1.TextChanged
  34        'Synchronise the label text and form text with the text in the text box
  35        Label1.Text = KryptonTextBox1.Text
  36        Label1.Left = 198.5 - (Label1.Width / 2)
  37        Me.Text = KryptonTextBox1.Text
  38    End Sub
  39
  40    Private Sub countdown_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
  41        'Save the log
  42        My.Settings.logCountdown.Clear()
  43        For Each item In KryptonListBox1.Items
  44            My.Settings.logCountdown.Add(item)
  45        Next
  46    End Sub
  47
  48    Private Sub countdown_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  49        'Load the log and set default values
  50        For Each item In My.Settings.logCountdown
  51            KryptonListBox1.Items.Add(item)
  52        Next
  53        If My.Settings.hideExtra = False Then
  54            Label1.Text = "00:00:00.00000"
  55            KryptonTextBox1.Text = "00:05:00.00000"
  56        Else
  57            Label1.Text = "00:00:00.0"
  58            KryptonTextBox1.Text = "00:05:00.0"
  59        End If
  60        Label1.Text = KryptonTextBox1.Text
  61    End Sub
  62
  63    Private Sub KryptonButton4_Click(sender As Object, e As EventArgs) Handles KryptonButton4.Click
  64        'Show/hide the log
  65        If Me.Size.Width = 650 Then
  66            Me.MinimumSize = New Size(0, 0)
  67            Me.MaximumSize = New Size(0, 0)
  68            Me.Width = 397
  69            Me.MinimumSize = New Size(397, 286)
  70            Me.MaximumSize = New Size(397, 286)
  71            KryptonButton4.Text = "Show Log"
  72        Else
  73            Me.MinimumSize = New Size(0, 0)
  74            Me.MaximumSize = New Size(0, 0)
  75            Me.Width = 650
  76            Me.MinimumSize = New Size(650, 286)
  77            Me.MaximumSize = New Size(650, 286)
  78            KryptonButton4.Text = "Hide Log"
  79            KryptonListBox1.Focus()
  80        End If
  81    End Sub
  82
  83    Private Sub KryptonButton5_Click(sender As Object, e As EventArgs) Handles KryptonButton5.Click
  84        'Log the current time
  85        My.Settings.countdownLogID = My.Settings.countdownLogID + 1
  86        KryptonListBox1.Items.Add("Log " & My.Settings.countdownLogID.ToString & " | " & Label1.Text)
  87        Me.MinimumSize = New Size(0, 0)
  88        Me.MaximumSize = New Size(0, 0)
  89        Me.Width = 650
  90        Me.MinimumSize = New Size(650, 286)
  91        Me.MaximumSize = New Size(650, 286)
  92        KryptonButton4.Text = "Hide Log"
  93        KryptonListBox1.Focus()
  94    End Sub
  95
  96    Private Sub KryptonButton6_Click(sender As Object, e As EventArgs) Handles KryptonButton6.Click
  97        'Count down from the selected log entry
  98        If Not KryptonListBox1.SelectedItem = "" Then
  99
 100            reset()
 101
 102            Dim selItem As String
 103            Dim strArr() As String
 104            strArr = KryptonListBox1.SelectedItem.Split("| ")
 105            selItem = (strArr(1).ToString)
 106            KryptonTextBox1.Text = selItem
 107
 108            startTime = System.DateTime.Now
 109            Timer1.Start()
 110            KryptonButton9.Text = "Pause"
 111            KryptonButton9.Focus()
 112        End If
 113    End Sub
 114
 115    Private Sub KryptonButton7_Click(sender As Object, e As EventArgs) Handles KryptonButton7.Click
 116        'Delete the selected log entry
 117        Dim lst As New List(Of Object)
 118        For Each a As Object In KryptonListBox1.SelectedItems
 119            lst.Add(a)
 120        Next
 121        For Each a As Object In lst
 122            KryptonListBox1.Items.Remove(a)
 123        Next
 124    End Sub
 125
 126    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
 127        'Copy current time
 128        Clipboard.SetText(Label1.Text)
 129    End Sub
 130
 131    Private Sub KryptonListBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles KryptonListBox1.KeyDown
 132        'Delete selected item with DELETE key
 133        If e.KeyCode = Keys.Delete Then
 134            'Remove the selected logged time
 135            Dim lst As New List(Of Object)
 136            For Each a As Object In KryptonListBox1.SelectedItems
 137                lst.Add(a)
 138            Next
 139            For Each a As Object In lst
 140                KryptonListBox1.Items.Remove(a)
 141            Next
 142        End If
 143        If e.Control And e.KeyCode.ToString = "A" Then
 144            'Select all logged items
 145            For i As Integer = 0 To KryptonListBox1.Items.Count - 1
 146                KryptonListBox1.SetSelected(i, True)
 147            Next
 148        End If
 149    End Sub
 150
 151    Private Sub KryptonListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles KryptonListBox1.SelectedIndexChanged
 152        'Disables/enables "Start at Selected" button based on selection
 153        If KryptonListBox1.SelectedItems.Count > 1 Then
 154            KryptonButton6.Enabled = False
 155        Else
 156            KryptonButton6.Enabled = True
 157        End If
 158    End Sub
 159
 160    Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenToolStripMenuItem.Click
 161        'Show Me
 162        Me.WindowState = FormWindowState.Normal
 163    End Sub
 164
 165    Private Sub StartTimingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StartTimingToolStripMenuItem.Click
 166        'Start the timer
 167        start()
 168        Me.WindowState = FormWindowState.Normal
 169    End Sub
 170
 171    Private Sub PauseTimingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PauseTimingToolStripMenuItem.Click
 172        'Pause
 173        pause()
 174        Me.WindowState = FormWindowState.Normal
 175    End Sub
 176
 177    Private Sub ResetTimingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ResetTimingToolStripMenuItem.Click
 178        reset()
 179        Me.WindowState = FormWindowState.Normal
 180    End Sub
 181
 182    Private Sub LogTimeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LogTimeToolStripMenuItem.Click
 183        'Log the current time
 184        KryptonListBox1.Items.Add(Label1.Text)
 185        Me.Width = 650
 186        KryptonButton4.Text = "Hide Log"
 187        Me.WindowState = FormWindowState.Normal
 188    End Sub
 189
 190    Private Sub RenameToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RenameToolStripMenuItem.Click
 191        'Show the rename dialog
 192        renameCountdown.Show()
 193    End Sub
 194
 195    Private Sub StartAtSelectedToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StartAtSelectedToolStripMenuItem.Click
 196        'Start at the selected time
 197        If Not KryptonListBox1.SelectedItem = "" Then
 198            Dim selItem As String
 199            Dim strArr() As String
 200            strArr = KryptonListBox1.SelectedItem.Split("| ")
 201            selItem = (strArr(1).ToString)
 202            KryptonTextBox1.Text = selItem
 203            If Label1.Text = KryptonTextBox1.Text Or hasBeenPaused = True Then
 204                Timer2.Stop()
 205                If hasBeenPaused = False Then
 206                    startTime = System.DateTime.Now
 207                Else
 208                    elapsedPauseAddends = (elapsedPause)
 209                End If
 210                Timer1.Start()
 211            End If
 212        End If
 213    End Sub
 214
 215    Private Sub DeleteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeleteToolStripMenuItem.Click
 216        'Delete the selected log entry
 217        If Not KryptonListBox1.SelectedItem = "" Then
 218            Dim lst As New List(Of Object)
 219            For Each a As Object In KryptonListBox1.SelectedItems
 220                lst.Add(a)
 221            Next
 222            For Each a As Object In lst
 223                KryptonListBox1.Items.Remove(a)
 224            Next
 225        End If
 226    End Sub
 227
 228    Private Sub countdown_Shown(sender As Object, e As EventArgs) Handles Me.Shown
 229        'Show/hide log automatically
 230        If My.Settings.showLogDefault = True Then
 231            Me.MinimumSize = New Size(0, 0)
 232            Me.MaximumSize = New Size(0, 0)
 233            Me.Width = 650
 234            Me.MinimumSize = New Size(650, 286)
 235            Me.MaximumSize = New Size(650, 286)
 236            KryptonButton4.Text = "Hide Log"
 237            KryptonListBox1.Focus()
 238        End If
 239    End Sub
 240
 241    Private Sub KryptonButton9_Click_1(sender As Object, e As EventArgs) Handles KryptonButton9.Click
 242        If KryptonButton9.Text = "Start" Then
 243            'Start at:
 244            hasBeenPaused = False
 245            Dim matches As MatchCollection = New Regex("^((\d?\d)?\:(?=\d*\:))?((\d?\d)?\:)?(\d?\d(\.\d*)?)?$").Matches(KryptonTextBox1.Text)
 246            Dim output As String
 247            Try
 248                For Each value As Integer In {2, 4, 5} ' Index of current working value
 249                    Dim val = matches(0).Groups(value).Value ' Current working value
 250                    If Not value = 5 Then ' Format hours, minutes
 251                        If val = "" Then
 252                            val = "00"
 253                        End If
 254                        val = val + ":"
 255                    Else ' Format seconds
 256                        If val = "" Then
 257                            val = "00.0"
 258                        End If
 259                    End If
 260                    output = output + val
 261                Next
 262                countDownFrom = TimeSpan.Parse(output)
 263                start()
 264            Catch ex As Exception
 265                Dim result As DialogResult = MessageBox.Show("Check that 'Count down from' is in an acceptable format." & Microsoft.VisualBasic.vbNewLine & "Press OK to see acceptable formats.", "Invalid Format", MessageBoxButtons.OKCancel)
 266
 267                If result = Windows.Forms.DialogResult.OK Then
 268                    Process.Start("https://etechtimer.codeplex.com/wikipage?title=Acceptable%20Input%20Formats")
 269                End If
 270            End Try
 271        ElseIf KryptonButton9.Text = "Resume" Then
 272            start()
 273        ElseIf KryptonButton9.Text = "Pause" Then
 274            pause()
 275        End If
 276    End Sub
 277
 278    Private Sub KryptonButton2_Click(sender As Object, e As EventArgs) Handles KryptonButton2.Click
 279        reset()
 280    End Sub
 281
 282#End Region
 283
 284#Region "Timer Functionality"
 285    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
 286        Try
 287            'Happens every 1ms...
 288
 289            'Update elapsed time:
 290            elapsedTime = countDownFrom - ((DateTime.Now - startTime) - elapsedPause)
 291            If My.Settings.hideExtra = True Then
 292                Label1.Text = elapsedTime.ToString.Substring(0, elapsedTime.ToString.IndexOf(".") + 2) 'Hide extra characters
 293                Label1.Left = 198.5 - (Label1.Width / 2) 'Center the label in the form
 294                Me.Text = elapsedTime.ToString.Substring(0, elapsedTime.ToString.IndexOf(".") + 2) 'Update form text
 295                NotifyIcon1.Text = elapsedTime.ToString.Substring(0, Label1.Text.IndexOf(".") + 2) & " - E-Tech Timer" 'Update systray icon
 296            Else
 297                Label1.Text = elapsedTime.ToString 'Update label
 298                Label1.Left = 198.5 - (Label1.Width / 2) 'Center label in form
 299                Me.Text = Label1.Text 'Update form text
 300                NotifyIcon1.Text = elapsedTime.ToString & " - E-Tech Timer" 'Update systray icon
 301            End If
 302
 303            'Check if the program should alert the user:
 304            If Label1.Text = "00:00:00.00000" Or Label1.Text = "00:00:00.0" Then
 305                If My.Settings.keepCounting = False Then 'Check if the timer should keep going...
 306                    Timer1.Stop() 'If not, stop the clock
 307                End If
 308
 309                'Play and show the alert:
 310                Dim Sound As New System.Media.SoundPlayer()
 311                If My.Settings.alertName = "Horn" Then
 312                    Sound.Stream = My.Resources.horn
 313                ElseIf My.Settings.alertName = "Morning" Then
 314                    Sound.Stream = My.Resources.morning
 315                ElseIf My.Settings.alertName = "Tinkle" Then
 316                    Sound.Stream = My.Resources.tinkle
 317                End If
 318                Sound.Load()
 319                Sound.Play()
 320                NotifyIcon1.ShowBalloonTip(1000, "Time's up! - E-Tech Timer", "The timer has reached 0. Click to open E-Tech Timer.", ToolTipIcon.Info)
 321                KryptonButton9.Text = "Start"
 322            End If
 323
 324        Catch ex As Exception
 325
 326        End Try
 327    End Sub
 328
 329    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
 330        'Happens every 1ms WHEN THE TIMER IS PAUSED
 331        elapsedPause = (DateTime.Now - startPause) + elapsedPauseAddends
 332    End Sub
 333
 334    Sub start()
 335        'Start the timer
 336        Timer2.Stop()
 337        If KryptonTextBox1.Text = "00:00:00.00000" Or KryptonTextBox1.Text = "00:00:00.0" Then              'If timer is set to 0...
 338            MsgBox("Please enter a timspan above 0 in the 'Countdown From' textbox.")                       'Notify the user
 339        Else                                                                                                'If timer is NOT set to 0...
 340            If hasBeenPaused = False Then                                                                   '..and the user wants to START A NEW SESSION
 341                reset()
 342                startTime = System.DateTime.Now                                                             'Save the current system time
 343            Else                                                                                            '...and  the user wants to RESUME TIMING
 344                elapsedPauseAddends = elapsedPause                                                          'Save how long the timer has been paused for
 345            End If
 346
 347            Timer1.Start()                                                                                  'Start the timer (unconditional as long as input time is not equal to 0)
 348            KryptonButton9.Text = "Pause"
 349
 350        End If
 351    End Sub
 352
 353    Sub pause()
 354        'Pause the timer
 355        Timer1.Stop()
 356        startPause = DateTime.Now
 357        Timer2.Start()
 358        hasBeenPaused = True
 359        KryptonButton9.Text = "Resume"
 360    End Sub
 361
 362    Sub reset()
 363        'Reset the clock
 364        Timer1.Stop()
 365        Timer2.Stop()
 366        elapsedTime = Nothing
 367        elapsedPause = Nothing
 368        elapsedPauseAddends = Nothing
 369        Label1.Text = KryptonTextBox1.Text
 370        hasBeenPaused = False
 371        Label1.Left = 198.5 - (Label1.Width / 2)
 372        KryptonButton9.Text = "Start"
 373    End Sub
 374
 375
 376#End Region
 377
 378End Class